﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Hellquest
{
    public class Sprite : Component
    {
        protected Texture2D texture;
        private Rectangle? sourceRect;

        public bool IsCentered;
        public Texture2D Texture { get { return texture; } }
        public Rectangle? SourceRectangle { get { return sourceRect; } set { sourceRect = value; } }
        public Vector2 Origin
        {
            get
            {
                if (IsCentered)
                    return new Vector2(Texture.Bounds.Center.X, Texture.Bounds.Center.Y);

                return Vector2.Zero;
            }
        }

        public SpriteEffects Effects;
        public float Depth;
        public bool Perspective;
        public float Scale;

        public Vector2 drawingOffset;
        public Color MixColor;

        public int Width 
        { 
            get 
            {
                return Texture.Width; 
            } 
        }
        public int Height 
        { 
            get 
            {
                return Texture.Height; 
            } 
        }

        public Sprite()
        {
            Effects = SpriteEffects.None;
            Depth = 0f;
            Scale = 1f;
            MixColor = Color.White;
            IsCentered = false;
            Perspective = false;

            drawingOffset = new Vector2();
        }

        public Sprite(Texture2D _texture, Rectangle? _sourceRect, bool _isCentered = false)
        {
            texture = _texture;
            sourceRect = _sourceRect;
            Effects = SpriteEffects.None;
            Depth = 0f;
            Scale = 1f;
            MixColor = Color.White;
            IsCentered = _isCentered;
            Perspective = false;

            drawingOffset = new Vector2();
        }

        public Sprite(Texture2D _texture, bool _isCentered = false)
        {
            texture = _texture;
            sourceRect = null;
            Effects = SpriteEffects.None;
            Depth = 0f;
            Scale = 1f;
            MixColor = Color.White;
            IsCentered = _isCentered;
            Perspective = false;

            drawingOffset = new Vector2();
        }

        public Sprite(Texture2D _texture, bool _perspective, bool _isCentered = false)
        {
            texture = _texture;
            sourceRect = null;
            Effects = SpriteEffects.None;
            Depth = 0;
            Scale = 1f;
            MixColor = Color.White;
            IsCentered = _isCentered;
            Perspective = _perspective;

            drawingOffset = new Vector2();
        }

        public Sprite(Texture2D _texture, bool _perspective, Rectangle? _sourceRect, float _depth, bool _isCentered = false)
        {
            texture = _texture;
            sourceRect = _sourceRect;
            Effects = SpriteEffects.None;
            Depth = _depth;
            Scale = 1f;
            MixColor = Color.White;
            IsCentered = _isCentered;
            Perspective = _perspective;

            drawingOffset = new Vector2();
        }

        public override void OnAdd(GameObject _gameObject)
        {
            base.OnAdd(_gameObject);
            gameObject.sprite = this;
            Graphics.Instance.AddSprite(this);
        }

        public override void OnRemove()
        {
            base.OnRemove();
            gameObject.sprite = null;
            Graphics.Instance.RemoveSprite(this);
        }
    }
}
